<?php

namespace app\teacher\controller;


use app\admin\model\classes\Student;
use app\common\model\ClassesCourseInfo;
use think\Db;
use think\Validate;
use app\common\model\ClassesCourseTable as ClassesCourseTableModel;
use app\admin\model\classes\Classes;
use app\common\model\ClassesSignLog;

class ClassesCourseTable extends Base
{
    protected $model = null;
    public function __construct(ClassesCourseTableModel $model)
    {
        parent::__construct();
        $this->model = $model;
    }
    public function index()
    {
        $page = $this->request->get('page', 1);
        $limit = $this->request->get('limit', 10);

        $this->success('获取成功', [
            'count' =>  $this->model->count(),
            'list' =>  $this->model->with(['classes', 'info'])->order('id desc')->page($page, $limit)->select()
        ]);

    }
    
    public function info()
    {
        $id = $this->request->get('id', null);
        $info = $this->model->with(['classes', 'info'])->order('id desc')->find($id);
        if (!$info) $this->error('数据不存在');
        $this->success($info);
    }

    public function edit()
    {
        $id = $this->request->post('id', 0);
        //参数验证
        $rule = [
            'classes_id'   => 'require',
            'start_time' => 'require',
            'end_time' => 'require',
//            'question_id'  => 'require',
        ];

        $msg = [
            'classes_id.require' => '请选择班级',
            'start_time.require' => '请选择起始时间',
            'end_time.require' => '请选择结束时间',
//            'question_id.require' => '请选择题库',
        ];
        $data = $this->request->post();
        $courseInfo = isset($data['course_info']) && $data['course_info']?$data['course_info']:[];
        unset($data['course_info']);
        $validate = new Validate($rule, $msg);
        if (!$validate->check($data)) $this->error($validate->getError());

        //验证班级是否有效
        $classes = Classes::where('id', $data['classes_id'])->find();
        if (!$classes) $this->error('班级不存在');
        if ($classes['is_end_course'] == 1) $this->error('当前班级已结课');

        //变更签到记录数据
        $model = new ClassesCourseTableModel();
        $title = $id?'编辑':'新增';
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);

        //课程表详情
        if (!$courseInfo) $this->error('课程详情不能为空');
        Db::startTrans();
        if ($id) {
            // 修改
            $detail = $model->where('id', $id)->find();
            $res = $detail->save($data);
        } else {
            //新增
            $res = $model->save($data);
            if ($res !== false)
                $id = $model->getLastInsID();
        }

        if ($res === false) {
            Db::rollback();
            $this->error($title.'失败');
        }
        foreach ($courseInfo as $k => $v) {
            $courseInfo[$k]['course_id'] = $id;
        }

        $classesCourseInfoModel = new ClassesCourseInfo();
        $delRes = $classesCourseInfoModel->where('course_id', $id)->delete();
        if ($delRes === false)  {
            Db::rollback();
            $this->error($title.'失败!');
        }
        $courseInfoRes = $classesCourseInfoModel->saveAll($courseInfo);
        if ($courseInfoRes === false)  {
            Db::rollback();
            $this->error($title.'失败!~');
        }

        Db::commit();
        $this->success($title.'成功', ['id' => $id]);
    }


}